package com.ivan;

import android.content.DialogInterface;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;

import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.FragmentActivity;

import com.ivan.easyphotos.EasyPhotos;
import com.ivan.easyphotos.R;
import com.ivan.easyphotos.callback.SelectCallback;
import com.ivan.easyphotos.constant.Capture;
import com.ivan.easyphotos.constant.CaptureEvent;
import com.ivan.easyphotos.constant.Type;
import com.ivan.easyphotos.engine.CompressEngine;
import com.ivan.easyphotos.engine.ImageEngine;
import com.ivan.easyphotos.engine.RecognitionEngine;
import com.ivan.easyphotos.models.album.entity.Photo;
import com.ivan.easyphotos.setting.Setting;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;


public class EasyImageSelect implements OnClickListener {
    private ImageBuilder imageBuilder;
    private VideoBuilder videoBuilder;
    private AlertDialog mDialog;



    public static ImageBuilder withImage(FragmentActivity context){
        return new ImageBuilder(context);
    }


    public static VideoBuilder withVideo(FragmentActivity context){
        return new VideoBuilder(context);
    }


    public static void previewImages(FragmentActivity context,ArrayList<String> paths,int currentIndex){
        EasyPhotos.startPreviewPaths(context, GlideEngine.getInstance(), paths, true,currentIndex);
    }




    public interface ImageSelectCallBack{

        void onImageSelectResult(List<String> imgList,List<Photo> photos);

        void onDismiss();

    }




    private EasyImageSelect( ImageBuilder imageBuilder) {
        this.imageBuilder = imageBuilder;
    }

    private EasyImageSelect( VideoBuilder builder) {
        this.videoBuilder = builder;
    }


    public void show() {
        Setting.isInEasyPage=true;
        if (mDialog == null) {
            mDialog = new AlertDialog.Builder(imageBuilder.context).create();

            Window window = mDialog.getWindow();
            window.setBackgroundDrawableResource(android.R.color.white);  //解决5.0以上手机dialog无法横向占满屏幕的问题
            window.getAttributes().gravity = Gravity.BOTTOM;
            window.getDecorView().setBackgroundColor(Color.TRANSPARENT);

            LayoutInflater inflater = LayoutInflater.from(imageBuilder.context);
            View contentView = inflater.inflate(R.layout.easy_image_select, null);
            initView(contentView);

            mDialog.show();
            mDialog.setContentView(contentView);
            mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {

                    if (imageBuilder.callBack!=null){
                        imageBuilder.callBack.onDismiss();
                    }
                }
            });
        } else {
            mDialog.show();
        }
    }


    public void openImageAlbum(){
        EasyPhotos.createAlbum(imageBuilder.context, true, imageBuilder.imageEngine !=null? imageBuilder.imageEngine:GlideEngine.getInstance())
                .setCount(imageBuilder.maxImageNumber)
                .setPuzzleMenu(false)
                .setCleanMenu(false)
                .isCompress(imageBuilder.isNeedCompress)
                .setCompressEngine(imageBuilder.compressEngine !=null? imageBuilder.compressEngine:LubanCompressEngine.getInstance())
                .isCrop(false)
                .isRecognite(imageBuilder.isNeedRecognite)
                .setRecognitionEngine(imageBuilder.recognitionEngine)
                .filter(Type.image())
                .start(new SelectCallback() {
                    @Override
                    public void onResult(ArrayList<Photo> photos, ArrayList<String> paths, boolean isOriginal) {
                        if (imageBuilder.callBack!=null){
                            imageBuilder.callBack.onImageSelectResult(paths,photos);
                        }
                        release();
                    }
                });
    }

    public void captureImage(){
        EasyPhotos.createCamera(imageBuilder.context)
                .enableSystemCamera(!imageBuilder.isUseCustomCamera)
                .isCrop(true)
                .setFreeStyleCropEnabled(true)
                .enableSingleCheckedBack(true)
                .setAspectRatio(1,1)
                .isCompress(imageBuilder.isNeedCompress)
                .setCompressEngine(imageBuilder.compressEngine!=null? imageBuilder.compressEngine:LubanCompressEngine.getInstance())
                .isRecognite(imageBuilder.isNeedRecognite)
                .setRecognitionEngine(imageBuilder.recognitionEngine)
                .setHideBottomControls(true)
                .setCapture(Capture.IMAGE)
                .start(new SelectCallback() {
                    @Override
                    public void onResult(ArrayList<Photo> photos, ArrayList<String> paths, boolean isOriginal) {
                        if (imageBuilder.callBack!=null){
                            imageBuilder.callBack.onImageSelectResult(paths,photos);
                        }
                        release();
                    }
                });
    }


    public void openVideoAlbum(){
        EasyPhotos.createAlbum(videoBuilder.context, true, videoBuilder.imageEngine !=null?videoBuilder.imageEngine:GlideEngine.getInstance())
                .setCount(videoBuilder.maxImageNumber)
                .setPuzzleMenu(false)
                .setCleanMenu(false)
                .isCompress(videoBuilder.isNeedCompress)
                .setCompressEngine(videoBuilder.compressEngine !=null?videoBuilder.compressEngine:LubanCompressEngine.getInstance())
                .filter(Type.video())
                .start(new SelectCallback() {
                    @Override
                    public void onResult(ArrayList<Photo> photos, ArrayList<String> paths, boolean isOriginal) {
                        if (videoBuilder.callBack!=null){
                            videoBuilder.callBack.onImageSelectResult(paths,photos);
                        }
                        release();
                    }
                });
    }


    public void filmVideo(){
        EasyPhotos.createCamera(videoBuilder.context)
                .enableSystemCamera(!videoBuilder.isUseCustomCamera)
                .isCompress(videoBuilder.isNeedCompress)
                .setCompressEngine(videoBuilder.compressEngine!=null?videoBuilder.compressEngine:LubanCompressEngine.getInstance())
                .setHideBottomControls(true)
                .setCapture(Capture.VIDEO)
                .start(new SelectCallback() {
                    @Override
                    public void onResult(ArrayList<Photo> photos, ArrayList<String> paths, boolean isOriginal) {
                        if (videoBuilder.callBack!=null){
                            videoBuilder.callBack.onImageSelectResult(paths,photos);
                        }
                        release();
                    }
                });
    }

    private void initView(View contentView) {
        if (imageBuilder.isOnlyCapture){
            contentView.findViewById(R.id.selectBtn).setVisibility(View.GONE);
            contentView.findViewById(R.id.cameraBtn).setBackgroundResource(R.drawable.easy_shape_image_bg_4);
            contentView.findViewById(R.id.line1).setVisibility(View.GONE);
        }
        contentView.findViewById(R.id.cameraBtn).setOnClickListener(this);
        contentView.findViewById(R.id.selectBtn).setOnClickListener(this);
        contentView.findViewById(R.id.cancelBtn).setOnClickListener(this);
    }

    private void gotoCameraActivity(){

        EventBus.getDefault().post(new CaptureEvent());  //考试需要

        if (imageBuilder !=null){
            EasyPhotos.createCamera(imageBuilder.context)
                    .enableSystemCamera(!imageBuilder.isUseCustomCamera)
                    .isCrop(true)
                    .setFreeStyleCropEnabled(true)
                    .enableSingleCheckedBack(true)
                    .setAspectRatio(16,9)
                    .isCompress(imageBuilder.isNeedCompress)
                    .setCompressEngine(imageBuilder.compressEngine!=null? imageBuilder.compressEngine:LubanCompressEngine.getInstance())
                    .isRecognite(imageBuilder.isNeedRecognite)
                    .setRecognitionEngine(imageBuilder.recognitionEngine)
                    .setHideBottomControls(true)
                    .setFilpCamera(imageBuilder.isfilpCamera)
                    .setCapture(Capture.IMAGE)
                    .start(new SelectCallback() {
                        @Override
                        public void onResult(ArrayList<Photo> photos, ArrayList<String> paths, boolean isOriginal) {
                            if (imageBuilder.callBack!=null){
                                imageBuilder.callBack.onImageSelectResult(paths,photos);
                            }
                            release();
                        }
                    });
        }

        if (videoBuilder!=null){
            EasyPhotos.createCamera(videoBuilder.context)
                    .enableSystemCamera(!videoBuilder.isUseCustomCamera)
                    .isCompress(videoBuilder.isNeedCompress)
                    .setCompressEngine(videoBuilder.compressEngine!=null?videoBuilder.compressEngine:LubanCompressEngine.getInstance())
                    .setHideBottomControls(true)
                    .setCapture(Capture.VIDEO)
                    .start(new SelectCallback() {
                        @Override
                        public void onResult(ArrayList<Photo> photos, ArrayList<String> paths, boolean isOriginal) {
                            if (videoBuilder.callBack!=null){
                                videoBuilder.callBack.onImageSelectResult(paths,photos);
                            }
                            release();
                        }
                    });
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.cameraBtn) {
            gotoCameraActivity();


        } else if (id == R.id.selectBtn) {

            if (imageBuilder !=null){
                EasyPhotos.createAlbum(imageBuilder.context, true, imageBuilder.imageEngine !=null? imageBuilder.imageEngine:GlideEngine.getInstance())
                        .setCount(imageBuilder.maxImageNumber)
                        .setPuzzleMenu(false)
                        .setCleanMenu(false)
                        .isCompress(imageBuilder.isNeedCompress)
                        .isRecognite(imageBuilder.isNeedRecognite)
                        .setRecognitionEngine(imageBuilder.recognitionEngine)
                        .setCompressEngine(imageBuilder.compressEngine !=null? imageBuilder.compressEngine:LubanCompressEngine.getInstance())
                        .isCrop(false)
                        .filter(Type.image())
                        .start(new SelectCallback() {
                            @Override
                            public void onResult(ArrayList<Photo> photos, ArrayList<String> paths, boolean isOriginal) {
                                if (imageBuilder.callBack!=null){
                                    imageBuilder.callBack.onImageSelectResult(paths,photos);
                                }
                                release();
                            }
                        });

            }

            if (videoBuilder!=null){
                EasyPhotos.createAlbum(videoBuilder.context, true, videoBuilder.imageEngine !=null?videoBuilder.imageEngine:GlideEngine.getInstance())
                        .setCount(videoBuilder.maxImageNumber)
                        .setPuzzleMenu(false)
                        .setCleanMenu(false)
                        .isRecognite(imageBuilder.isNeedRecognite)
                        .setRecognitionEngine(imageBuilder.recognitionEngine)
                        .isCompress(videoBuilder.isNeedCompress)
                        .setCompressEngine(videoBuilder.compressEngine !=null?videoBuilder.compressEngine:LubanCompressEngine.getInstance())
                        .filter(Type.video())
                        .start(new SelectCallback() {
                            @Override
                            public void onResult(ArrayList<Photo> photos, ArrayList<String> paths, boolean isOriginal) {
                                if (videoBuilder.callBack!=null){
                                    videoBuilder.callBack.onImageSelectResult(paths,photos);
                                }
                                release();
                            }
                        });
            }

        }

        mDialog.dismiss();

    }


    private void release(){
        if (imageBuilder!=null){
            imageBuilder.context = null;
            imageBuilder.callBack = null;
            imageBuilder = null;
        }


        if (videoBuilder!=null){
            videoBuilder.context = null;
            videoBuilder.callBack = null;
            videoBuilder = null;
        }
    }

    public static class ImageBuilder {
        private int maxImageNumber;
        private FragmentActivity context;
        private ImageSelectCallBack callBack;
        private boolean isNeedCompress;
        private boolean isNeedRecognite;
        private boolean isUseCustomCamera;
        private RecognitionEngine recognitionEngine;
        private CompressEngine compressEngine;
        private ImageEngine imageEngine;
        private boolean isOnlyCapture;
        private boolean isfilpCamera;

        public ImageBuilder setOnlyCapture(boolean onlyCapture) {
            isOnlyCapture = onlyCapture;
            return this;
        }

        public ImageBuilder setIsfilpCamera(boolean isfilpCamera) {
            this.isfilpCamera = isfilpCamera;
            return this;
        }

        public ImageBuilder setNeedRecognite(boolean needRecognite) {
            isNeedRecognite = needRecognite;
            return this;
        }

        public ImageBuilder setRecognitionEngine(RecognitionEngine recognitionEngine) {
            this.recognitionEngine = recognitionEngine;
            return this;
        }

        public ImageBuilder setCompressEngine(CompressEngine compressEngine) {
            this.compressEngine = compressEngine;
            return this;
        }

        public ImageBuilder setImageEngine(ImageEngine imageEngine) {
            this.imageEngine = imageEngine;
            return this;
        }

        public ImageBuilder setNeedCompress(boolean needCompress) {
            isNeedCompress = needCompress;
            return this;
        }


        public ImageBuilder setUseCustomCamera(boolean useCustomCamera) {
            isUseCustomCamera = useCustomCamera;
            return this;
        }

        public ImageBuilder setCallBack(ImageSelectCallBack callBack) {
            this.callBack = callBack;
            return this;
        }

        public ImageBuilder(FragmentActivity context){
            this.context = context;
        }


        public ImageBuilder setMaxImageNumber(int maxImageNumber) {
            this.maxImageNumber = maxImageNumber;
            return this;
        }

        public void show(){
            new EasyImageSelect(this).show();
        }

        public void openImageAlbum(){
            new EasyImageSelect(this).openImageAlbum();
        }

        public void captureImage(){
            new EasyImageSelect(this).captureImage();
        }

    }


    public static class VideoBuilder {
        private int maxImageNumber;
        private FragmentActivity context;
        private ImageSelectCallBack callBack;
        private boolean isNeedCompress;
        private boolean isUseCustomCamera;
        private CompressEngine compressEngine;
        private ImageEngine imageEngine;



        public VideoBuilder setCompressEngine(CompressEngine compressEngine) {
            this.compressEngine = compressEngine;
            return this;
        }

        public VideoBuilder setImageEngine(ImageEngine imageEngine) {
            this.imageEngine = imageEngine;
            return this;
        }

        public VideoBuilder setNeedCompress(boolean needCompress) {
            isNeedCompress = needCompress;
            return this;
        }


        public VideoBuilder setUseCustomCamera(boolean useCustomCamera) {
            isUseCustomCamera = useCustomCamera;
            return this;
        }

        public VideoBuilder setCallBack(ImageSelectCallBack callBack) {
            this.callBack = callBack;
            return this;
        }

        public VideoBuilder(FragmentActivity context){
            this.context = context;
        }


        public VideoBuilder setMaxImageNumber(int maxImageNumber) {
            this.maxImageNumber = maxImageNumber;
            return this;
        }

        public void show(){
            new EasyImageSelect(this).show();
        }

        public void filmVideo(){
            new EasyImageSelect(this).filmVideo();
        }

        public void openVideoAlbum(){
            new EasyImageSelect(this).openVideoAlbum();
        }


    }
}
